package com.ie.sevice.before;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.comment.http.ResponseResult;
import com.ie.entity.CartEntity;
import com.ie.mapper.before.CartMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartEntity> implements CartService {

    @Override
    public ResponseResult<Map<String, Object>> addCart(CartEntity cartEntity) {
        Long goodsNum = lambdaQuery().eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId()).count();
        boolean result = false;
        if (goodsNum > 0) {
            //更新购物车数量
            LambdaUpdateWrapper<CartEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CartEntity::getShoppingnum, cartEntity.getShoppingnum()+1)
                    .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())
                    .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId());
            result=update(updateWrapper);
        }else {//添加新商品到购物车
            result=save(cartEntity);
        }
        if (result) {
            return ResponseResult.getMessageResult(null,"A001");
        }else {
            return ResponseResult.getMessageResult(null,"A002");
        }
    }

    @Override
    @Transactional
    public ResponseResult<Map<String, Object>> bupDateCart(CartEntity cartEntity) {

        List<Integer> bcid = cartEntity.getBcid();
        List<Integer> bshoppingnum = cartEntity.getBshoppingnum();
        ArrayList<CartEntity> cartEntities = new ArrayList<CartEntity>();
        for (int i = 0; i < bcid.size(); i++) {
            CartEntity cartEntity1 = new CartEntity();
            cartEntity1.setId(bcid.get(i));
            cartEntity1.setShoppingnum(bshoppingnum.get(i));
            cartEntities.add(cartEntity1);
        }
        boolean b = updateBatchById(cartEntities);
        if (b) {
            return ResponseResult.getMessageResult(null,"A001");
        }else {
            return ResponseResult.getMessageResult(null,"A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> clearCart(CartEntity cartEntity) {
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<CartEntity>();
        queryWrapper.eq("busertable_id",cartEntity.getBusertableId());
        boolean b = remove(queryWrapper);
        if (b) {
            return ResponseResult.getMessageResult(null,"A001");
        }else {
            return ResponseResult.getMessageResult(null,"A002");
        }
    }
}